package shanghaielectric_lib.RDBHelper.DataPushHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import shanghaielectric_lib.RDBHelper.Cell;
import shanghaielectric_lib.RDBHelper.Cell_show;


/**
 * Created by cuihanbing on 2017/11/2.
 */

public class ObserverManager implements SubjectListener {

    private static ObserverManager observerManager;
    //观察者接口集合
    private List<ObserverListener_UI> list_UI = new ArrayList<>();
    private List<ObserverListener_Write> list_Write = new ArrayList<>();

    /**
     * 单例
     */
    public static ObserverManager getInstance() {
        if (null == observerManager) {
            synchronized (ObserverManager.class) {
                if (null == observerManager) {
                    observerManager = new ObserverManager();
                }
            }
        }
        return observerManager;
    }

    /**
     * 加入监听队列---read
     */
    @Override
    public void add_UI(ObserverListener_UI observerListenerUI) {
        list_UI.add(observerListenerUI);
    }

    /**
     * 通知观察者刷新界面数据---read
     */
    @Override
    public void notifyObserver_UI(Map<Integer, Map<Integer, Cell_show>> mapMap) {
        for (ObserverListener_UI observerListenerUI : list_UI) {
            observerListenerUI.update_UI(mapMap);
        }
    }


    /**
     * 监听队列中移除---read
     */
    @Override
    public void remove_UI(ObserverListener_UI observerListenerUI) {
        if (list_UI.contains(observerListenerUI)) {
            list_UI.remove(observerListenerUI);
        }
    }


    /**
     * 加入监听队列---write
     */

    @Override
    public void add_write(ObserverListener_Write observerListenerWrite) {
        list_Write.add(observerListenerWrite);
    }

    /**
     * 通知观察者通信数据---write
     */
    @Override
    public void notifyObserver_Write(Map<Integer, Map<Integer, Cell>> mapMap) {
        for (ObserverListener_Write observerListenerWrite : list_Write) {
            observerListenerWrite.update_Write(mapMap);
        }
    }

    /**
     * 监听队列中移除---write
     */
    @Override
    public void remove_Write(ObserverListener_Write observerListenerWrite) {
        if (list_Write.contains(observerListenerWrite)) {
            list_Write.remove(observerListenerWrite);
        }
    }

}
